#include "listview.h"

ListModel::ListModel()
{

}

int ListModel::rowCount(const QModelIndex &parent) const
{
    return list.size();
}

QVariant ListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }

    int nRow = index.row();
    if (nRow>= rowCount() || nRow<0)
    {
        return QVariant();
    }

    switch (role)
    {
        case DataRole:
        {
            return list[nRow].data;
            break;
        }
        case IconRole:
        {
            return list[nRow].icon;
        }
    }

    return QVariant();
}

bool ListModel::setData(const QModelIndex &index, const QVariant &, int role)
{
    if (index.isValid() && role == Qt::EditRole)
    {
        //qDebug() << "setData : " << index.row();
        //发送信号触发刷新
        emit dataChanged(index, index, QVector<int>() << role);
        return true;
    }
    return false;
}

void ListModel::addList(const QList<KeyInfo> &list)
{
    this->list.clear();
    beginResetModel();
    this->list = list;
    endResetModel();
}

void ListModel::addData(QString data, QString icon)
{
    KeyInfo info;
    info.data = data;
    info.icon = icon;
    beginResetModel();
    list.push_back(info);
    endResetModel();
}

void ListModel::clear()
{
    beginResetModel();
    list.clear();
    endResetModel();
}

Delegate::Delegate(QObject *parent): QStyledItemDelegate(parent)
{

}

void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QRect rect = QRect(option.rect.left()+5, option.rect.top(), 73, 73);

    QString data = index.data(ListModel::DataRole).toString();
    QString icon = index.data(ListModel::IconRole).toString();

    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor("#EBEBFF"));
    painter->drawRect(option.rect);
    painter->restore();
    if(icon == "")
    {
        QFont font;

        painter->save();
        if(data=="123")
        {
            //painter->setBrush(QColor(114,114,241,150));
            painter->setBrush(QColor("#7272F1"));
            font.setPixelSize(36);
        }
        else
        {
            painter->setBrush(QColor("#7272F1"));
            font.setPixelSize(44);
        }
        painter->setPen(Qt::NoPen);
        painter->drawRoundedRect(rect, 8, 8);
        painter->restore();

        painter->save();
        painter->setPen(QPen(Qt::white));
        painter->setFont(font);
        painter->drawText(rect, Qt::AlignCenter, data);
        painter->restore();
    }
    else
    {
        painter->save();
        QPixmap pix(icon);
        painter->drawPixmap(rect, pix);
        painter->restore();
    }
}

QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QSize(73+5*2, 73+5);
}

bool Delegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    QRect rect = option.rect;
    QMouseEvent *e = static_cast<QMouseEvent*>(event);
    if(rect.contains(e->pos()))
    {
        if(event->type() == QEvent::MouseButtonRelease)
        {
            if(!isMove)
            {
                QString data = model->data(index, ListModel::DataRole).toString();
                qDebug() << data;
                emit itemClicked(data);
            }

            isPress = false;
            isMove = false;
        }
        else if(event->type() == QEvent::MouseMove)
        {
            if(isPress)
            {
                if((e->pos().x() - pressPos.x() > 10) || (e->pos().y() - pressPos.y() > 10) || \
                    e->pos().x() - pressPos.x() < -10 || (e->pos().y() - pressPos.y() < -10))
                {
                    if(!isMove)
                    {
                        isMove = true;
                    }
                }
            }
        }
        else if(event->type() == QEvent::MouseButtonPress)
        {
            isPress = true;
            pressPos = e->pos();
        }
    }
    return QStyledItemDelegate::editorEvent(event, model, option, index);
}

ListView::ListView(QWidget *parent) : QListView(parent)
{
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->setDragEnabled(false);
    this->setVerticalScrollMode(QListView::ScrollPerPixel);
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->setFocusPolicy(Qt::NoFocus);
    setViewMode(QListView::IconMode);
    setFlow(QListView::TopToBottom);

    QScroller::grabGesture(this, QScroller::TouchGesture);
    QScroller *m_scroller = QScroller::scroller(this);
    QScrollerProperties properties = m_scroller->scrollerProperties();
    properties.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);//关闭越区
    // properties.setScrollMetric(QScrollerProperties::DragVelocitySmoothingFactor, 1.0);
    properties.setScrollMetric(QScrollerProperties::ScrollingCurve, 23);
    properties.setScrollMetric(QScrollerProperties::MousePressEventDelay, 2);
    // properties.setScrollMetric(QScrollerProperties::DecelerationFactor, 2.0);
    m_scroller->setScrollerProperties(properties);
    //connect(m_scroller, &QScroller::stateChanged, this, &TableListing::stateChanged);

}
